# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userAmountLogDao  import UserAmountLogDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

"""
        表名：zzh_ueser_amount_log

        说明：会员金额日志表
"""

@app.route('/queryUserAmountLogById', methods=['POST'])
def queryUserAmountLogById():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON= UserAmountLogDao().queryUserAmountLogById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserAmountLog OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserAmountLogByCondition', methods=['POST'])
def queryUserAmountLogByCondition():
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    try:
        total_count, UserCodeJSON = UserAmountLogDao().queryUserAmountLogByCondition(pageIndex=pageIndex, pageSize=pageSize,conditions=conditions, multiSort=multiSort)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserAmountLog OK"},"total":total_count,"info":UserCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertUserAmountLog', methods=['POST'])
def insertUserAmountLog():

    if not request.json:
        abort(400)

    Info = {

        'userId': request.json.get('userId'),  #
        'userName': request.json.get('userName'),  #
        'paymentId': request.json.get('paymentId'),  #
        'value': request.json.get('value'),#
        'reamrk':request.json.get('reamrk')


    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserAmountLogDao().addUserAmountLog(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserAmount OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserAmount Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/updateUserAmountLog', methods=['POST'])
def updateUserAmountLog():#
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = UserAmountLogDao().selectUserAmountLog(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
         'id': request.json.get('id'),  #
        'userId': request.json.get('userId',oldInfo[1]),  #
        'userName': request.json.get('userName',oldInfo[2]),  #
        'paymentId': request.json.get('paymentId',oldInfo[3]),  #
        'value': request.json.get('value',oldInfo[3]),#
        'reamrk':request.json.get('reamrk',oldInfo[5])

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserAmountLogDao().updateUserAmountLog(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateUserAmount OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateUserAmount Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteUserAmountLog', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserAmountLog():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = UserAmountLogDao().deleteUserAmountLog(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUser OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUser Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


